package some.talk.util;

import java.io.File;

public class FileUtil {
	//占쎈굝以덌옙占썲칰�덉쨮
	public static final String UPLOAD_PATH = "C:/javawork/workspace_spring/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/ch13-myBatisBoard/upload";
	 
	/**
	 * 占쎈슣��占쎈�已�癰귨옙瑗� 癰귨옙瑗랃옙占쏙옙��カ占쏙옙筌롫뗄�쇽옙�뺢땀占쎈Ŋ苑�筌욑옙��
	 * */
	public static String rename(String filename) throws Exception{
		 
		if(filename ==null) return null;
		String newName = 
Long.toString(System.currentTimeMillis())+(int)(Math.random()*50);
		
		return rename(filename, newName);
	}
	/**
	 * 占쎈슣��占쎈�已�癰귨옙瑗� 癰귨옙瑗랃옙占쏙옙��カ占쏙옙筌롫뗄�쇽옙占쏙옙紐꾪뀱占쏙옙占쎈슢彛륅옙占쏙쭪占쎌젟 揶쏉옙��
	 * */
	public static String rename(String filename, String newName) 
	                                            throws Exception{
		
		if(filename == null) return null;

		//占쎈슣�わ쭗�놁뱽 占쎈�釉�옙占쏙옙類ㅻ뻼占쎌눖以�癰귨옙瑗랃옙�띾┛
		int idx = filename.lastIndexOf(".");
		String extention = "";
		String newFileName = "";

		if(idx != -1) {
			extention = filename.substring(idx);
		}
		// 占쎈벡�ｏ옙癒�옙 占쎌뮇�낉옙占쏙옙占쏙옙��뵬筌뤿굞��占쎈굝�곤옙�곷튊 占쎌꼶�쀯옙占쏙옙類ㅼ삢占쎈Ŧ占�占싼뗫맙占쎈똻苑�
		// 占쎈굝�곤옙�덈뼄筌롳옙占쎈벡�ｏ옙癒�옙 占쎌뮇�낉옙�랁� 占쎈슣�わ쭗�놁몵嚥∽옙占쎈뿮��
		int newIdx = newName.lastIndexOf(".");
		if(newIdx !=-1){
			newName = newName.substring(0,newIdx);
		}

		newFileName = newName + extention.toLowerCase();
		
		return newFileName;
	}
	/**
	 * 占쎈슣��占쏙옙��
	 * */
	public static void removeFile(String filename){
		File file = new File(UPLOAD_PATH,filename);
		if(file.exists()) file.delete();
		
	}
}
